package gr.unipi.interfaces;

import gr.unipi.cinema.SqlConnection;

import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.configuration.XMLConfiguration;

public class ConfigurationListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        File configFile;

        try {
            configFile = new File(context.getRealPath("/WEB-INF/CinemaProject.xml"));
            //System.out.println(configFile.getAbsolutePath());
            XMLConfiguration config = new XMLConfiguration(configFile);
            //config.setFile();
            System.out.println("Found configuration file at " + configFile.getAbsolutePath());
            
            SqlConnection.config = config;
        } catch (Exception ex) {
        	ex.printStackTrace();
        	System.out.println("Configuration file CinemaProject.xml could not be found. Using default values");
            //Logger.getLogger(ConfigurationListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {}
}
